package priv.bruce.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import priv.bruce.entry.UserInfo;
import priv.bruce.util.HandlerSet;
import priv.bruce.util.JDBCUtil;

public class UserService implements HandlerSet<UserInfo>{
	public UserInfo validUserInfo(String username) throws Exception {
		String sql = "select * from t_user where user_name=?";
		List<UserInfo> query = JDBCUtil.query(sql,this,username);
		if (query.size()==1) {
			return query.get(0);
		}else {
			return null;
		}
	}
	
	public UserInfo adminLogin(String username) throws Exception {
		String sql = "select * from t_user where user_name=? and quanxian = 0";
		List<UserInfo> query = JDBCUtil.query(sql,this,username);
		if (query.size()==1) {
			return query.get(0);
		}else {
			return null;
		}
	}
	
	public int addUser(String username,String password,String addr,String mail,String phone) throws Exception {
		
		//insert into user(username,password,email) values(?,?,?)
		String sql="insert into t_user(user_name,password,user_phone,user_addr,user_mail)values(?,?,?,?,?)";
		return JDBCUtil.update(sql, username,password,phone,addr,mail);
	}
	
	public int resetPassword(String phone,String newpassword) throws Exception {
		// TODO Auto-generated method stub

		String sql="update t_user set password=? where user_phone=?";
		return JDBCUtil.update(sql,newpassword, phone);
	}
	
	public int modifyInformation(int id,String username,String password,String mail,String phone,String addr) throws Exception
	{
		String sql="update t_user set user_name=?,password=?,user_mail=?,user_phone=?,user_addr=? where user_id=?";
		return JDBCUtil.update(sql,username, password,mail,phone,addr,id);
		
	}
	
	public List<UserInfo> selectByName(String username) throws Exception {
		String sql = "SELECT * FROM t_user WHERE user_name LIKE CONCAT('%',?,'%')";
		return JDBCUtil.query(sql, new HandlerSet<UserInfo>() {
			@Override
			public UserInfo handler(ResultSet rs) throws SQLException {
				UserInfo userInfo = new UserInfo();
				userInfo.setUser_id(rs.getInt("user_id"));
				userInfo.setUser_name(rs.getString("user_name"));
//				userInfo.setPassword(rs.getString("password"));
				userInfo.setUser_phone(rs.getString("user_phone"));
				userInfo.setUser_addr(rs.getString("user_addr"));
				userInfo.setUser_mail(rs.getString("user_mail"));
				userInfo.setUser_enable(rs.getInt("user_enable"));
				userInfo.setQuanxian(rs.getInt("quanxian"));
				return userInfo;
			}
		}, username);
	}
	
	public int lockUserById(int id) throws Exception {
		String sql="UPDATE t_user SET user_enable = 0 WHERE user_id = ?";
		return JDBCUtil.update(sql, id);
	}
	
	public int unlockUserById(int id) throws Exception {
		String sql="UPDATE t_user SET user_enable = 1 WHERE user_id = ?";
		return JDBCUtil.update(sql, id);
	}
	
	@Override
	public UserInfo handler(ResultSet rs) throws SQLException {
		UserInfo userInfo = new UserInfo();
		userInfo.setUser_id(rs.getInt("user_id"));
		userInfo.setUser_name(rs.getString("user_name"));
		userInfo.setPassword(rs.getString("password"));
		userInfo.setUser_phone(rs.getString("user_phone"));
		userInfo.setUser_addr(rs.getString("user_addr"));
		userInfo.setUser_mail(rs.getString("user_mail"));
		userInfo.setUser_enable(rs.getInt("user_enable"));
		userInfo.setQuanxian(rs.getInt("quanxian"));
		return userInfo;
	}
}
